import json
import os
import requests

list = '''
{
    "code": 0,
    "msg": "操作成功",
    "data": {
        "contentData": {
            "contentInfo": [
                {
                    "column_info": "",
                    "created_at": "2021-09-15 23:00:00",
                    "id": "l_61262a55e4b01c99903cfe03",
                    "img_url": "https://wechatapppro-1252524126.file.myqcloud.com/app9a6JfVbz4568/image/khzytjxo0b49c68ljbff.png",
                    "img_url_compressed": "http://wechatapppro-1252524126.file.myqcloud.com/app9a6JfVbz4568/image/compress/414320931khzytjxo0b49c68ljbff.png",
                    "img_url_compressed_larger": "http://wechatapppro-1252524126.file.myqcloud.com/app9a6JfVbz4568/image/compress/640480185khzytjxo0b49c68ljbff.png",
                    "is_top": 0,
                    "is_try": 0,
                    "manual_stop_at": "",
                    "order_weight": 5,
                    "part_try_length": 0,
                    "product_id": "p_61de833ee4b0373be5661177",
                    "product_name": "趣味教育丨专场",
                    "product_type": 0,
                    "reference_id": "r_61de836ee4b09f1cc18e8b67",
                    "relation_state": 0,
                    "resource_created_at": "2021-08-25 19:32:37",
                    "resource_id": "l_61262a55e4b01c99903cfe03",
                    "resource_name": "趣味教育：如何教孩子玩&乐？01",
                    "resource_type": 4,
                    "resource_updated_at": "2021-12-26 17:05:56",
                    "shop_id": "app9a6JfVbz4568",
                    "show_in_menu": 1,
                    "start_at": "2021.09.15 22:00",
                    "state": 0,
                    "updated_at": "",
                    "zb_start_at": "2021-09-15 22:00:00",
                    "zb_stop_at": "2021-09-15 23:00:00",
                    "srcType": 4,
                    "title": "趣味教育：如何教孩子玩&乐？01",
                    "view_count": 2150,
                    "payment_type": 3,
                    "audio_compress_url": "",
                    "piece_price": 0,
                    "comment_count": 0,
                    "is_try_source": false,
                    "availableInfo": {
                        "paymentType": 3,
                        "productId": "p_61de833ee4b0373be5661177",
                        "resourceType": 4,
                        "resourceId": "l_61262a55e4b01c99903cfe03",
                        "piecePrice": 0,
                        "available": true
                    },
                    "learn_progress": 0,
                    "create_at": "2021-08-25 19:32:37",
                    "jump_url": "/content_page/eyJ0eXBlIjoyLCJyZXNvdXJjZV90eXBlIjo0LCJyZXNvdXJjZV9pZCI6ImxfNjEyNjJhNTVlNGIwMWM5OTkwM2NmZTAzIiwicHJvZHVjdF9pZCI6InBfNjFkZTgzM2VlNGIwMzczYmU1NjYxMTc3IiwiYXBwX2lkIjoiYXBwOWE2SmZWYno0NTY4In0",
                    "interval_start_at": "已结束"
                },
                {
                    "column_info": "",
                    "created_at": "2021-09-16 23:00:00",
                    "id": "l_61262a6be4b01c99903cfe18",
                    "img_url": "https://wechatapppro-1252524126.file.myqcloud.com/app9a6JfVbz4568/image/khzytjxo0b49c68ljbff.png",
                    "img_url_compressed": "http://wechatapppro-1252524126.file.myqcloud.com/app9a6JfVbz4568/image/compress/414320931khzytjxo0b49c68ljbff.png",
                    "img_url_compressed_larger": "http://wechatapppro-1252524126.file.myqcloud.com/app9a6JfVbz4568/image/compress/640480185khzytjxo0b49c68ljbff.png",
                    "is_top": 0,
                    "is_try": 0,
                    "manual_stop_at": "",
                    "order_weight": 4,
                    "part_try_length": 0,
                    "product_id": "p_61de833ee4b0373be5661177",
                    "product_name": "趣味教育丨专场",
                    "product_type": 0,
                    "reference_id": "r_61de836ee4b09f1cc18e8b68",
                    "relation_state": 0,
                    "resource_created_at": "2021-08-25 19:32:59",
                    "resource_id": "l_61262a6be4b01c99903cfe18",
                    "resource_name": "趣味教育：如何教孩子玩&乐？02",
                    "resource_type": 4,
                    "resource_updated_at": "2021-12-26 17:06:05",
                    "shop_id": "app9a6JfVbz4568",
                    "show_in_menu": 1,
                    "start_at": "2021.09.16 22:00",
                    "state": 0,
                    "updated_at": "",
                    "zb_start_at": "2021-09-16 22:00:00",
                    "zb_stop_at": "2021-09-16 23:00:00",
                    "srcType": 4,
                    "title": "趣味教育：如何教孩子玩&乐？02",
                    "view_count": 1430,
                    "payment_type": 3,
                    "audio_compress_url": "",
                    "piece_price": 0,
                    "comment_count": 0,
                    "is_try_source": false,
                    "availableInfo": {
                        "paymentType": 3,
                        "productId": "p_61de833ee4b0373be5661177",
                        "resourceType": 4,
                        "resourceId": "l_61262a6be4b01c99903cfe18",
                        "piecePrice": 0,
                        "available": true
                    },
                    "learn_progress": 0,
                    "create_at": "2021-08-25 19:32:59",
                    "jump_url": "/content_page/eyJ0eXBlIjoyLCJyZXNvdXJjZV90eXBlIjo0LCJyZXNvdXJjZV9pZCI6ImxfNjEyNjJhNmJlNGIwMWM5OTkwM2NmZTE4IiwicHJvZHVjdF9pZCI6InBfNjFkZTgzM2VlNGIwMzczYmU1NjYxMTc3IiwiYXBwX2lkIjoiYXBwOWE2SmZWYno0NTY4In0",
                    "interval_start_at": "已结束"
                },
                {
                    "column_info": "",
                    "created_at": "2021-09-18 23:00:00",
                    "id": "l_61262a99e4b065461cbed9f9",
                    "img_url": "https://wechatapppro-1252524126.file.myqcloud.com/app9a6JfVbz4568/image/khzytjxo0b49c68ljbff.png",
                    "img_url_compressed": "http://wechatapppro-1252524126.file.myqcloud.com/app9a6JfVbz4568/image/compress/414320931khzytjxo0b49c68ljbff.png",
                    "img_url_compressed_larger": "http://wechatapppro-1252524126.file.myqcloud.com/app9a6JfVbz4568/image/compress/640480185khzytjxo0b49c68ljbff.png",
                    "is_top": 0,
                    "is_try": 0,
                    "manual_stop_at": "",
                    "order_weight": 3,
                    "part_try_length": 0,
                    "product_id": "p_61de833ee4b0373be5661177",
                    "product_name": "趣味教育丨专场",
                    "product_type": 0,
                    "reference_id": "r_61de836ee4b09f1cc18e8b69",
                    "relation_state": 0,
                    "resource_created_at": "2021-08-25 19:33:45",
                    "resource_id": "l_61262a99e4b065461cbed9f9",
                    "resource_name": "趣味教育：如何训练孩子吃苦？",
                    "resource_type": 4,
                    "resource_updated_at": "2021-12-26 17:06:22",
                    "shop_id": "app9a6JfVbz4568",
                    "show_in_menu": 1,
                    "start_at": "2021.09.18 22:00",
                    "state": 0,
                    "updated_at": "",
                    "zb_start_at": "2021-09-18 22:00:00",
                    "zb_stop_at": "2021-09-18 23:00:00",
                    "srcType": 4,
                    "title": "趣味教育：如何训练孩子吃苦？",
                    "view_count": 1448,
                    "payment_type": 3,
                    "audio_compress_url": "",
                    "piece_price": 0,
                    "comment_count": 0,
                    "is_try_source": false,
                    "availableInfo": {
                        "paymentType": 3,
                        "productId": "p_61de833ee4b0373be5661177",
                        "resourceType": 4,
                        "resourceId": "l_61262a99e4b065461cbed9f9",
                        "piecePrice": 0,
                        "available": true
                    },
                    "learn_progress": 0,
                    "create_at": "2021-08-25 19:33:45",
                    "jump_url": "/content_page/eyJ0eXBlIjoyLCJyZXNvdXJjZV90eXBlIjo0LCJyZXNvdXJjZV9pZCI6ImxfNjEyNjJhOTllNGIwNjU0NjFjYmVkOWY5IiwicHJvZHVjdF9pZCI6InBfNjFkZTgzM2VlNGIwMzczYmU1NjYxMTc3IiwiYXBwX2lkIjoiYXBwOWE2SmZWYno0NTY4In0",
                    "interval_start_at": "已结束"
                },
                {
                    "column_info": "",
                    "created_at": "2021-09-19 23:00:00",
                    "id": "l_61262ab0e4b020185a9f5859",
                    "img_url": "https://wechatapppro-1252524126.file.myqcloud.com/app9a6JfVbz4568/image/khzytjxo0b49c68ljbff.png",
                    "img_url_compressed": "http://wechatapppro-1252524126.file.myqcloud.com/app9a6JfVbz4568/image/compress/414320931khzytjxo0b49c68ljbff.png",
                    "img_url_compressed_larger": "http://wechatapppro-1252524126.file.myqcloud.com/app9a6JfVbz4568/image/compress/640480185khzytjxo0b49c68ljbff.png",
                    "is_top": 0,
                    "is_try": 0,
                    "manual_stop_at": "",
                    "order_weight": 2,
                    "part_try_length": 0,
                    "product_id": "p_61de833ee4b0373be5661177",
                    "product_name": "趣味教育丨专场",
                    "product_type": 0,
                    "reference_id": "r_61de836ee4b09f1cc18e8b6a",
                    "relation_state": 0,
                    "resource_created_at": "2021-08-25 19:34:08",
                    "resource_id": "l_61262ab0e4b020185a9f5859",
                    "resource_name": "趣味教育：孩子的玩&乐？",
                    "resource_type": 4,
                    "resource_updated_at": "2021-12-26 17:06:38",
                    "shop_id": "app9a6JfVbz4568",
                    "show_in_menu": 1,
                    "start_at": "2021.09.19 22:00",
                    "state": 0,
                    "updated_at": "",
                    "zb_start_at": "2021-09-19 22:00:00",
                    "zb_stop_at": "2021-09-19 23:00:00",
                    "srcType": 4,
                    "title": "趣味教育：孩子的玩&乐？",
                    "view_count": 1058,
                    "payment_type": 3,
                    "audio_compress_url": "",
                    "piece_price": 0,
                    "comment_count": 0,
                    "is_try_source": false,
                    "availableInfo": {
                        "paymentType": 3,
                        "productId": "p_61de833ee4b0373be5661177",
                        "resourceType": 4,
                        "resourceId": "l_61262ab0e4b020185a9f5859",
                        "piecePrice": 0,
                        "available": true
                    },
                    "learn_progress": 0,
                    "create_at": "2021-08-25 19:34:08",
                    "jump_url": "/content_page/eyJ0eXBlIjoyLCJyZXNvdXJjZV90eXBlIjo0LCJyZXNvdXJjZV9pZCI6ImxfNjEyNjJhYjBlNGIwMjAxODVhOWY1ODU5IiwicHJvZHVjdF9pZCI6InBfNjFkZTgzM2VlNGIwMzczYmU1NjYxMTc3IiwiYXBwX2lkIjoiYXBwOWE2SmZWYno0NTY4In0",
                    "interval_start_at": "已结束"
                },
                {
                    "column_info": "",
                    "created_at": "2021-09-25 23:00:00",
                    "id": "l_61262c7fe4b09a591796a9bf",
                    "img_url": "https://wechatapppro-1252524126.file.myqcloud.com/app9a6JfVbz4568/image/khzytjxo0b49c68ljbff.png",
                    "img_url_compressed": "http://wechatapppro-1252524126.file.myqcloud.com/app9a6JfVbz4568/image/compress/414320931khzytjxo0b49c68ljbff.png",
                    "img_url_compressed_larger": "http://wechatapppro-1252524126.file.myqcloud.com/app9a6JfVbz4568/image/compress/640480185khzytjxo0b49c68ljbff.png",
                    "is_top": 0,
                    "is_try": 0,
                    "manual_stop_at": "",
                    "order_weight": 1,
                    "part_try_length": 0,
                    "product_id": "p_61de833ee4b0373be5661177",
                    "product_name": "趣味教育丨专场",
                    "product_type": 0,
                    "reference_id": "r_61de836ee4b09f1cc18e8b6b",
                    "relation_state": 0,
                    "resource_created_at": "2021-08-25 19:41:51",
                    "resource_id": "l_61262c7fe4b09a591796a9bf",
                    "resource_name": "趣味教育：梁启超先生如何教小孩子玩乐",
                    "resource_type": 4,
                    "resource_updated_at": "2021-12-26 17:06:59",
                    "shop_id": "app9a6JfVbz4568",
                    "show_in_menu": 1,
                    "start_at": "2021.09.25 22:00",
                    "state": 0,
                    "updated_at": "2021-09-25 22:41:13",
                    "zb_start_at": "2021-09-25 22:00:00",
                    "zb_stop_at": "2021-09-25 23:00:00",
                    "srcType": 4,
                    "title": "趣味教育：梁启超先生如何教小孩子玩乐",
                    "view_count": 1060,
                    "payment_type": 3,
                    "audio_compress_url": "",
                    "piece_price": 0,
                    "comment_count": 0,
                    "is_try_source": false,
                    "availableInfo": {
                        "paymentType": 3,
                        "productId": "p_61de833ee4b0373be5661177",
                        "resourceType": 4,
                        "resourceId": "l_61262c7fe4b09a591796a9bf",
                        "piecePrice": 0,
                        "available": true
                    },
                    "learn_progress": 0,
                    "create_at": "2021-08-25 19:41:51",
                    "jump_url": "/content_page/eyJ0eXBlIjoyLCJyZXNvdXJjZV90eXBlIjo0LCJyZXNvdXJjZV9pZCI6ImxfNjEyNjJjN2ZlNGIwOWE1OTE3OTZhOWJmIiwicHJvZHVjdF9pZCI6InBfNjFkZTgzM2VlNGIwMzczYmU1NjYxMTc3IiwiYXBwX2lkIjoiYXBwOWE2SmZWYno0NTY4In0",
                    "interval_start_at": "已结束"
                }
            ],
            "hastryresource": false
        },
        "staticData": {
            "icon": {
                "talkCountIcon": "/images/home_index/icon_talkNum.png",
                "playCountIcon": "/images/home_index/icon_playNum.png",
                "videoTypeIcon": "/images/home_index/icon_shipinb.png",
                "audioTypeIcon": "/images/home_index/icon_yinpinb.png",
                "textTypeIcon": "/images/home_index/icon_tuwenb.png",
                "aliveTypeIcon": "/images/home_index/icon_Live.png",
                "readCountIcon": "/images/home_index/icon_yueduliang.png",
                "activityTypeIcon": "/images/home_index/icon_activity.png"
            },
            "status": {
                "pageLoading": true,
                "moreInfoLoding": true
            }
        }
    }
}
'''

def heBingTsVideo(download_path,hebing_path):
    all_ts = os.listdir(download_path)
    with open(hebing_path, 'wb+') as f:
        for i in range(len(all_ts)):
            ts_video_path = os.path.join(download_path, all_ts[i])
            f.write(open(ts_video_path, 'rb').read())
    print("合并完成！！")

if __name__ == '__main__':
    download_path = os.getcwd() + "\\download"
    json_obj = json.loads(list)
    for item in json_obj['data']['contentData']['contentInfo']:
        id = item['resource_id']
        name = item['resource_name']
        url='https://app9a6jfvbz4568.h5.xiaoeknow.com/%s.m3u8'%id

        print(name)
        print(url)

        all_content = requests.get(url).text
        # print(all_content)

        tss = []
        splits = all_content.split("\n")
        for idx, line in enumerate(splits):
            if (line.startswith("https://")):
                tss.append(line)

        base = 100000
        i = base

        for ts_url in tss:
            i = i + 1
            res = requests.get(ts_url)
            # print(res)
            # print(res.text)
            c_fule_name = str(i)

            with open(os.path.join(download_path, c_fule_name + ".ts"), 'ab') as f:
                f.write(res.content)

            print("============", i - base, '/', len(tss), "============")

        heBingTsVideo(download_path, os.path.join(download_path, name+".mp4"))
        print("============end============")
        print('')


